package com.songcha.library_business.proxy.database

import android.os.FileUtils
import com.songcha.library_business.helper.DaoManagerHelper
import com.songcha.library_business.proxy.userinfo.UserInfoManager
import com.songcha.library_business.util.BookFileUtil
import com.songcha.library_common.LibraryCommon
import com.songcha.library_common.util.LogUtil
import com.songcha.library_common.util.TimeUtil
import com.songcha.library_database_douyue.bean.BookBrowseHistoryBean
import com.songcha.library_database_douyue.bean.BookDownloadDBBean
import com.songcha.library_database_douyue.greendao.BookBrowseHistoryBeanDao
import com.songcha.library_database_douyue.greendao.BookDownloadDBBeanDao
import com.songcha.library_database_douyue.greendao.BookShelfDBBeanDao
import com.songcha.library_database_douyue.manager.GreenDaoManager
import org.greenrobot.greendao.query.WhereCondition
import java.io.File

class BookDownloadDaoManager {
    companion object{

        @JvmStatic
        fun addBookDownload(bookId:String){
            try{
                val dao=GreenDaoManager.getInstance().bookDownloadDBBeanDao
                val bean=BookDownloadDBBean()
                bean.apply {
                    this.bookId=bookId
                    userId= UserInfoManager.userId
                }
                val insertQB=dao.queryBuilder()
                insertQB.where(BookDownloadDBBeanDao.Properties.BookId.eq(bookId))
                    .where(BookDownloadDBBeanDao.Properties.UserId.eq(UserInfoManager.userId))
                val list=insertQB.list()
                if(list.size==0){
                    dao.insert(bean)
                }else{
                    val bean=list[0]
                    dao.update(bean)
                }
            }catch (e:Exception){
                LogUtil.log("保存缓存书籍异常")
                e.printStackTrace()
            }

        }

        /***
         * whether book had downloaded
         */
        @JvmStatic
        fun hasBookDownloaded(bookId:String):Boolean{
            try{
                val dao=GreenDaoManager.getInstance().bookDownloadDBBeanDao
                val qb=dao.queryBuilder()
                qb.where(BookDownloadDBBeanDao.Properties.BookId.eq(bookId))
                    .where(BookDownloadDBBeanDao.Properties.UserId.eq(UserInfoManager.userId))
                val list=qb.list()
                return list.size>0
            }catch (e:Exception){
                e.printStackTrace()
                return false
            }
        }

        /***
         * whether book all download,distinguish chapter has update
         */
        @JvmStatic
        fun hasBookAllDownload(bookId:String,chapterCount:Int):Boolean{
            if (!hasBookDownloaded(bookId)) return false
            val file= BookFileUtil.getBookFile(bookId)
            var downloadCount=0
            if(file.exists() && file.isDirectory){
                val fileList=file.listFiles()
                if(fileList!=null){
                    for(item in fileList){
                        if(item.name.endsWith(".txt"))
                            downloadCount++
                    }
                }
            }
            return downloadCount>=chapterCount
        }

        @JvmStatic
        fun delBookDownloadByIds(ids:MutableList<String>){
            if(ids.size==0) return
            try{
                val dao=GreenDaoManager.getInstance().bookDownloadDBBeanDao
                val qb=dao.queryBuilder()
                qb.where(BookDownloadDBBeanDao.Properties.UserId.eq(UserInfoManager.userId))
                    .where(
                        WhereCondition.StringCondition(
                            BookDownloadDBBeanDao.Properties.BookId.columnName + " in " + DaoManagerHelper.getRangeStringByIds(
                                ids
                            )
                        )
                    )
                qb.buildDelete().executeDeleteWithoutDetachingEntities()
            }catch (e:Exception){
                e.printStackTrace()
            }
        }


    }
}